package cn.tedu._03vehicle_test.controller;

import cn.tedu._03vehicle_test.base.response.JsonResult;
import cn.tedu._03vehicle_test.base.response.StatusCode;
import cn.tedu._03vehicle_test.mapper.UserMapper;
import cn.tedu._03vehicle_test.pojo.dto.UserLoginParam;
import cn.tedu._03vehicle_test.pojo.dto.UserRegParam;
import cn.tedu._03vehicle_test.pojo.entity.User;
import cn.tedu._03vehicle_test.pojo.vo.UserLoginVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.Date;

@Api(tags = "02.用户管理模块")
@Slf4j
@RequestMapping("/v1/user/")
@RestController
public class UserController {
    @Autowired
    private UserMapper userMapper;
    @PostMapping("reg")
    @ApiOperation("用户注册功能")
    @ApiOperationSupport(order = 10)
    public JsonResult regUser(UserRegParam userRegParam){
        log.debug("userRegParam = "+userRegParam);
        String username = userRegParam.getUsername();
        int num = userMapper.selectUsername(username);
        if (num > 0){
            return new JsonResult(StatusCode.USERNAME_EXISTS);
        }
        User user = new User();
        BeanUtils.copyProperties(userRegParam,user);
        user.setCreateTime(new Date());
        user.setUpdateTime(new Date());
        log.debug("User = "+user);
        int i = userMapper.regUser(user);
        if (i > 0 ){
            return JsonResult.ok(userRegParam);
        }
        return new JsonResult(StatusCode.OPERATION_FAILED);
    }
    @PostMapping("login")
    @ApiOperation("用户登录功能")
    @ApiOperationSupport(order = 20)
    public JsonResult UserLogin(UserLoginParam userLoginParam, @ApiIgnore HttpSession session){
        log.debug("userloginPara"+userLoginParam);
        UserLoginVO userVO = userMapper.Loginselect(userLoginParam);
        log.debug("userVO="+userVO);
        if(userVO!= null){
            session.setAttribute("user",userVO);
            return JsonResult.ok(userVO);
        }else{
            return new JsonResult(StatusCode.USERNAME_PASSWORD_ERROR);
        }
    }

    @GetMapping("currentUser")
    @ApiOperation("获取用户信息")
    @ApiOperationSupport(order = 30)
    public JsonResult CurrentUser(@ApiIgnore HttpSession session) {
        log.debug("开始获取用户信息：");

        UserLoginVO userVO = (UserLoginVO) session.getAttribute("user");
        return JsonResult.ok(userVO);
    }
    @GetMapping("logout")
    @ApiOperation("退出登录")
    @ApiOperationSupport(order = 40)
    public void outLogin(@ApiIgnore HttpSession session){
        log.debug("退出登录开始执行");
        session.removeAttribute("user");
    }
}
